#! /bin/sh
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#
######################################################################
# This is the MVQR initialization script that is run when an
# instance is started or initialized. It launches tdm_arkqmm as a persistent
# process. The script can also be run manually, along with qmmstop, to
# stop and restart QMM while an instance is up.
#-----------------------------------------------------------------------
#
####################################################################

# -- tdm_arkqmm location
typeset qmmloc
qmmloc="$TRAF_HOME/export/bin32"

#--QMM process name
qmmName="ZQM0000"

# The script can start from anywhere
thome=`pwd`

# who is the current user
currentUser=`whoami`

# script name
qmmscr="$thome/startQmm"

# OUtput file
qmmlog="$thome/startQmm.log"

NULL=/dev/null
TRUE=1
FALSE=0

# by default we assume we are running on a cluster
thisIsACluster=1

# check if this is a real cluster
function checkCluster
{
  if [ -f $SQ_PDSH ]; then
     thisIsACluster=1
     echo "running the $0 script in a cluster ....."
     echo " "
  else
     thisIsACluster=0
     echo "running the $0 script in a workstation ....."
     echo " "
  fi
}

function startQMM
{
  # Remove script and its LOG file.
  rm $qmmlog > /dev/null 2>&1
  rm $qmmscr > /dev/null 2>&1

  # check if tdm_arkqmm is already running.
  if [[ $thisIsACluster = 1 ]]; then
    sqps | grep -e $qmmName > /dev/null 2>&1
  else
    ps -fu $currentUser | grep -e $qmmName | grep -v grep > /dev/null 2>&1
  fi

  result=$?
  # to make screen msgs look better
  echo " "

  if [[ $result = 0 ]]; then
    echo "tdm_arkqmm persistent process $qmmName is already running..." |  tee -a $qmmlog
    echo "Invoke the qmmstop script before starting again" |  tee -a $qmmlog
    echo " "
  else
    start_qmm;
    rm $qmmscr
  fi
}

function start_qmm
{

  # to make screen messages look better
  echo "Starting tdm_arkqmm as persistent process. Please wait...."
  echo " "

  # construct sqshell script
  cat > $qmmscr <<EOF
    sqshell -a <<eof
    persist exec QMN
    exit
eof
EOF
# NOTE: EOF on preceding line can have no leading or trailing spaces, or qmmstart
#       will get "unexpected end of file" when it is run.

  chmod 777 $qmmscr
  sh $qmmscr > $qmmlog  2>&1

  # while a while to allow for qms processes to start
  sleep 10

  # verify persistent process $qmmName started successfully
  if [[ $thisIsACluster = 1 ]]; then
    sqps | grep -e $qmmName > /dev/null 2>&1
  else
    ps -fu $currentUser | grep -e $qmmName | grep -v grep > /dev/null 2>&1
  fi

  result=$?

  #Check if return value of grep,
  #   0 is at least one match found,
  #   1 is no matches found,
  #   2 is error

  if [[ $result = 0 ]];then
     echo " " > $qmmlog
     echo "tdm_arkqmm process $qmmName started successfully." | tee -a $qmmlog
  else
     echo "Failed to start tdm_arkqmm as persistent process."
     echo "Please see $qmmlog for more details."
  fi

  # to make screen messages look better
  echo " " | tee -a $qmmlog
}


####################################################################
#                                                                  #
#                  Main Script starts here                         #
#                                                                  #
####################################################################
TITLEBAR=$0;

#check where we are running this script
checkCluster;

# Call the function to start QMM.
startQMM;

